/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upgmadendro.utils;

import upgmadendro.population.Population;

/**
 *
 * @author Shadev
 */
public class DistanceCalc implements DistanceCalculator<Population>{

    public double getDistance(Population a, Population b) {
        if(a.getVectorSize()!=b.getVectorSize()){
            throw new UnsupportedOperationException("Vector size doesn't match");
        }
        int commonAllels = 0;
        for(int i=0;i<a.getVectorSize();i++){
            if(a.getGenes().get(i)==b.getGenes().get(i)){
                commonAllels++;
            }
        }
        double size = a.getVectorSize();
        double res = (((double)commonAllels)*2)/(2*size);
        return round(1-res,4);
    }

    public static double round(double Rval, int Rpl) {
        double p = (double)Math.pow(10,Rpl);
        Rval = Rval * p;
        double tmp = Math.round(Rval);
        return (double)tmp/p;
    }

}
